package com.vcc.core.util;

import cn.idev.excel.EasyExcel;
import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.write.builder.ExcelWriterBuilder;
import cn.idev.excel.write.builder.ExcelWriterSheetBuilder;
import cn.idev.excel.write.metadata.WriteSheet;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class ExportUtil {


    /**
     * 获取导出文件名
     *
     * @param prefix 前缀
     * @return 导出文件名
     */
    public static String getExportFileName(String prefix) {
        return prefix + "_" + System.currentTimeMillis() + ".xlsx";
    }

    /**
     * 创建ExcelWriter实例
     *
     * @param fileName 导出文件名
     * @return ExcelWriter实例
     */
    public static ExcelWriter createExcelWriter(String fileName) {
        return EasyExcel.write(fileName)
                .build();
    }

    /**
     * 创建ExcelWriter实例，指定表头类
     *
     * @param outputStream 输出流
     * @return ExcelWriter实例
     */
    public static ExcelWriter createExcelWriter(OutputStream outputStream) {
        return EasyExcel.write(outputStream)
                .build();
    }

    /**
     * 创建ExcelWriter实例，指定表头类
     *
     * @param outputStream 输出流
     * @param headClass    表头类
     * @param <T>          表头类型
     * @return ExcelWriter实例
     */
    public static <T> ExcelWriter createExcelWriter(OutputStream outputStream, Class<T> headClass) {
        return EasyExcel.write(outputStream, headClass)
                .build();
    }

    /**
     * 创建ExcelWriter实例，指定表头类
     *
     * @param outputStream 输出流
     * @param headClass    表头类
     * @param <T>          表头类型
     * @return ExcelWriter实例
     */
    public static <T> ExcelWriter createExcelWriter(OutputStream outputStream, Class<T> headClass, Map<String, Boolean> headMap) {
        headMap.forEach((key,value) ->{
        });
        return EasyExcel.write(outputStream, headClass)
                .build();
    }


    public static void setExcelResponseHeaders(HttpServletResponse response, String fileName) {
        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
                .replaceAll("\\+", "%20");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 禁用缓存
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }

    /**
     * 关闭ExcelWriter
     *
     * @param excelWriter ExcelWriter实例
     */
    public static void closeExcelWriter(ExcelWriter excelWriter) {
        if (excelWriter != null) {
            excelWriter.close();
        }
    }


    /**
     * 创建WriteSheet实例
     *
     * @param sheetName 工作表名称
     * @return WriteSheet实例
     */
    public static WriteSheet createWriteSheet(String sheetName) {
        return EasyExcel.writerSheet(sheetName).build();
    }

    /**
     * 创建WriteSheet实例，指定表头类
     *
     * @param sheetName 工作表名称
     * @param headClass 表头类
     * @return WriteSheet实例
     */
    public static WriteSheet createWriteSheet(String sheetName, Class<?> headClass) {
        return EasyExcel.writerSheet(sheetName)
                .head(headClass)
                .build();
    }

    /**
     * 设置WriteSheet的表头
     *
     * @param writeSheet WriteSheet实例
     * @param headClass  表头类
     * @return 更新后的WriteSheet实例
     */
    public static WriteSheet setHeader(WriteSheet writeSheet, Class<?> headClass) {
        return EasyExcel.writerSheet(writeSheet.getSheetNo())
                .head(headClass)
                .build();
    }

    /**
     * 写入数据到Excel
     *
     * @param excelWriter ExcelWriter实例
     * @param writeSheet  WriteSheet实例
     * @param data        数据列表
     * @param <T>         数据类型
     */
    public static <T> void writeData(ExcelWriter excelWriter, WriteSheet writeSheet, List<T> data) {
        excelWriter.write(data, writeSheet);
    }


}
